package doantotnghiep.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import doantotnghiep.business.CabinetBusiness;
import doantotnghiep.model.Cabinet;
import doantotnghiep.modelDTO.CabinetDTO;

@Controller
public class CabinetController {
	@ModelAttribute("CabinetDTO")
	public CabinetDTO getCabinetObject() {
		return new CabinetDTO();
	}
	
	private Cabinet cabinet;
	
	private CabinetDTO cabinetDTO;
	@Autowired
	private CabinetBusiness cabinetBusiness;
	
	@RequestMapping(value = "/addCabinet", method= RequestMethod.POST)
	public String addCabinet(HttpServletRequest request, ModelMap model,
			@ModelAttribute("CabinetDTO") CabinetDTO cabinetDTO) {
		if (cabinetDTO != null) {
			cabinet = new Cabinet();
			
			cabinet.setStatus(cabinetDTO.getStatus());
			cabinet.setCabinetName(cabinetDTO.getCabinetName());
			cabinet.setNumberOfBatch(cabinetDTO.getNumberOfBatch());
			
			this.cabinetBusiness.addCabinet(cabinet);
			return "redirect:/viewCabinet";
		}
		return "viewCabinet";
	}
	
	@RequestMapping(value = "/addCabinet", method= RequestMethod.GET)
	public ModelAndView addCabinet(){
	
		return new ModelAndView("addCabinet", "",null);
	}
	
	@RequestMapping(value = "/deleteCabinet", method = RequestMethod.GET)
	public String deleteCabinet(HttpServletRequest request, ModelMap model,
			  int id) {
		cabinetBusiness.deleteCabinet(id);
		return "redirect:/viewCabinet";
	}
	
	@RequestMapping(value = "/viewCabinet", method= RequestMethod.GET)
	public ModelAndView viewCabinets(){
		List<Cabinet> cabinets;
		List<CabinetDTO> cabinetDTOs = new ArrayList<CabinetDTO>();
		if(cabinetBusiness != null){
			cabinets = cabinetBusiness.listCabinet();
			
			if(cabinets != null){
				for (Cabinet c : cabinets) {
					cabinetDTO = new CabinetDTO();
					
					cabinetDTO.setId(c.getId());
					cabinetDTO.setStatus(c.getStatus());
					cabinetDTO.setCabinetName(c.getCabinetName());
					cabinetDTO.setNumberOfBatch(c.getNumberOfBatch());
					
					cabinetDTOs.add(cabinetDTO);
				}
				Map<String, Object> listAllCabinet = new HashMap<String, Object>();
				listAllCabinet.put("cabinetDTOs", cabinetDTOs);
				return new ModelAndView("viewCabinet", "listAllCabinet",listAllCabinet);
			}
		}
		return new ModelAndView("viewCabinet","",null);
	}
	
	@RequestMapping(value = "/editCabinet", method= RequestMethod.GET)
	public ModelAndView editCabinet(HttpServletRequest request, ModelMap model,int id){
		cabinet = cabinetBusiness.getCabinetById(id);
		if(cabinet != null){
			cabinetDTO = new CabinetDTO();
			
			cabinetDTO.setId(cabinet.getId());
			cabinetDTO.setStatus(cabinet.getStatus());
			cabinetDTO.setCabinetName(cabinet.getCabinetName());
			cabinetDTO.setNumberOfBatch(cabinet.getNumberOfBatch());
			
		   return new ModelAndView("updateCabinet", "cabinetDTO", cabinetDTO);
		}
		return new ModelAndView("viewcabinet","",null);
	}
	
	@RequestMapping(value = "/editCabinet", method = RequestMethod.POST)
	public String editCabinet(HttpServletRequest request, ModelMap model,
			@ModelAttribute("CabinetDTO") CabinetDTO cabinetDTO){
		if (cabinetDTO != null) {
			cabinet= new Cabinet();
			
			cabinet.setId(cabinetDTO.getId());
			cabinet.setStatus(cabinetDTO.getStatus());
			cabinet.setCabinetName(cabinetDTO.getCabinetName());
			cabinet.setNumberOfBatch(cabinetDTO.getNumberOfBatch());
		    this.cabinetBusiness.saveCabinet(cabinet);
		}
		return "redirect:/viewCabinet";	
	}
}
